import re

import facade
from xjlibrary.our_file_dir import BaseDir

curPath = BaseDir.get_file_dir_absolute(__file__)
TopPath = BaseDir.get_upper_dir(curPath, -2)
sPath = BaseDir.get_new_path(TopPath, "download", "shusheng_cqjy", "download", "listhtml")
configfile = BaseDir.get_new_path(curPath, "db.ini")

class ParaList(object):

    def __init__(self):
        self.logger = facade.get_streamlogger()
        self.url_regex = re.compile(r"(book_detail.action\?bookId=(\d+)&kindId=&kindCode=\d+)")
        self.mysqlutils = facade.MysqlUtiles(configfile, "db", logger=self.logger)

    def select(self):
        for file in BaseDir.get_dir_all_files(sPath):
            txt = BaseDir.single_read_file(file, encoding="GB18030")
            lines = self.url_regex.findall(txt)
            sql = "insert into shusheng(url,bookid)Values(%s,%s)"
            self.mysqlutils.ExeSqlMany(sql,lines)


if __name__ == "__main__":
    para = ParaList()
    para.select()


